import time
import csv
import os


class DataLogger:
    def __init__(self, log_file='operation_log.csv'):

        self.log_file = log_file

        self._init_log_file()

    def _init_log_file(self):
        """初始化日志文件表头[2]()"""
        with open(self.log_file, 'a', newline='') as f:
            writer = csv.writer(f)
            header = ['时间', '操作类型', '变量名', '新值', '稳定时间', '状态']
            if os.path.getsize(self.log_file) == 0:
                writer.writerow(header)

    def log_operation(self, op_type, var_name, new_val, time_value,status):
        """记录操作日志[5]()"""
        timestamp = time.strftime("%Y-%m-%d  %H:%M:%S")
        with open(self.log_file, 'a', newline='') as f:
            writer = csv.writer(f)
            writer.writerow([timestamp, op_type, var_name,  new_val, time_value,status])